package {

	import net.box2fp.Box2DEntity;
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.utils.Key;
	import net.flashpunk.utils.Input;
	import net.flashpunk.FP;
	import net.flashpunk.graphics.Spritemap;
	import net.box2fp.graphics.SuperGraphiclist;

	public class Boomerang extends Entity{

		[Embed(source = "assets/boomerang.png")]
		public const BoomerangImg:Class;
		//private var boomerang:Image = new Image(BoomerangImg);
		public var boomerang:Spritemap = new Spritemap(BoomerangImg, 16,16);
		
		public function Boomerang():void {
			graphic = boomerang;
			boomerang.add("rotate", [0,1,2,3], 24, true);
			boomerang.play("rotate");
			setHitbox(16,16);
			//(graphic as SuperGraphiclist).add(boomerang);
			visible = false;
		}
		
		private var timer:Number = 0;
		private var status:String = "",direction:String = "";
		
		override public function update():void {
			
			super.update();
			var pressed:Boolean = false;
			
			if (Input.released(Key.SPACE) && status == "") {
				x = MyBox2DWorld.player.x + 5;
				y = MyBox2DWorld.player.y ;
				direction = MyBox2DWorld.player.direction;
				visible = true;
				status = "start";
				trace("pos");
			}
			
			if (visible && !timer && (status == "start")) {
				timer = 1;
				status = "forward";
				trace("start");
			}
			
			if (visible && (timer > 0) && (status == "forward")) {
				
				if(direction == "right") {
					x+=3;
				}
				if(direction == "left") {
					x-=3;
				}
				timer -= FP.elapsed;
				trace("forward");
				
			}
			
			if (visible && (timer < 0) && (status == "forward")) {
				status = "returnBack";
				trace("toggle");
			}
			
			if (visible && status == "returnBack") {trace("back")
				if (x < MyBox2DWorld.player.x) {
					x+=3;
				}
				if (x > MyBox2DWorld.player.x) {
					x-=3;
				}
				if (y < MyBox2DWorld.player.y) {
					y+=3;
				}
				if (y > MyBox2DWorld.player.y) {
					y-=3;
				}
			}
			if (collide("player", x, y) && (status == "returnBack")){
				visible = false;
				status = "";
				timer = 0;
				trace("stop");
			}
			//trace(timer);
		}
		
		override public function render():void {
			super.render();
		}
	}

}	